package com.dwzlab.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dwzlab.vo.XUser;


/**
 * @author zhuxiaodong
 *
 */
public class DialogAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String opt = request.getParameter("opt");
		String str ="{" +
				"\"statusCode\":\"200\"," +
				"\"message\":\"ok\","+
				"\"navTabId\":\"dialog2\","+
				"\"rel\":\"\","+
				"\"callbackType\":\"\","+
				"\"forwardUrl\":\"\","+
				"\"confirmMsg\":\"\""+
				"}";
		
		
		if("list".equals(opt)){
			request.getRequestDispatcher("dialog2.jsp").forward(request, response);
		}
		if("set".equals(opt)){
			List<XUser> xUsers = new ArrayList<XUser>();
			for(int i=0;i<5;i++){
				XUser xUser = new XUser();
				xUser.setId(i);
				xUser.setE_name("merry"+i);
				xUsers.add(xUser);
			}
			request.getSession().setAttribute("users", xUsers);
			
			response.getWriter().write(str);
		}
		if("del".equals(opt)){
			int id = Integer.parseInt(request.getParameter("id"));
			@SuppressWarnings("unchecked")
			List<XUser> xUsers = (List<XUser>) request.getSession().getAttribute("users");
			for(XUser user : new ArrayList<XUser>(xUsers)){
				if(user.getId() == id){
					xUsers.remove(user);
					break;
				}
			}
			response.getWriter().write(str);
		}
	}

}
